<script>
	import store from '@/store'
	import { timeOut } from '@/utils/timeout';
	import {
		getToken
	} from '@/utils/auth'
	export default {
		data() {
			return {
				timer: null,
				updater: {
					"isCheckVersion": false,
					"innerVer": 0,
					"version": 0
				}
			};
		},

		methods: {
			showGlobalPopup() {
				//console.log("123456")
			},
			init() {
				const self = this
				const token = getToken()
				if (token) {
					store.dispatch('GetInfo').then(res => {
						const seller = "seller"
						const roles = store.getters && store.getters.roles
						const hasRole = roles.some(role => {
							return seller === role
						})
						if (!hasRole) {
							modal.msgError("仅支持[售货员]角色登录")
							store.dispatch('LogOut')
							return
						}
					})
				} else {
					uni.reLaunch({
						url: '/pages/login'
					})
				}
			},

			handleCheckVersion() {
				let _this = this.updater;
				// #ifdef APP-PLUS 
				_this.isCheckVersion = true;
				plus.screen.lockOrientation('portrait-primary') // 竖屏锁定
				plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
					_this.innerVer = widgetInfo.version;
					_this.version = widgetInfo.versionCode;
					uni.request({
						url: config.baseUrl + '/seller/upgrade?_t=' + new Date().getTime(), //版本检测
						data: {},
						header: {},
						success: (result) => {
							if (result.data.code === 0) {
								setTimeout(() => {
									_this.isCheckVersion = false;
								}, 1000);
								if (result.data.version - _this.version > 0) { // 如果最新版本大于现在已经安装的App的版本
									uni.showModal({
										title: "更新提示",
										content: "发现新版本,请确认下载更新?",
										success: (res) => {
											if (res.confirm) {
												uni.showLoading({
													title: '下载更新包中...'
												});
												uni.downloadFile({
													url: result.data.url,
													success: (downloadResult) => {
														uni.hideLoading();
														if (downloadResult
															.statusCode === 200
														) {
															plus.runtime
																.install(
																	downloadResult
																	.tempFilePath, {
																		force: true
																	},
																	function() {
																		console
																			.log(
																				'App安装成功！'
																			);
																		uni.showModal({
																			title: 'App安装成功!',
																			showCancel: false
																		})
																		plus.runtime
																			.restart();
																	},
																	function(
																		e) {
																		console
																			.log(
																				'App安装失败！'
																			);
																	})
														}
													}
												});
											}
										}
									})
								} else {
									uni.showModal({
										title: '当前已是最新版本',
										showCancel: false
									})
								}
							}
						}
					})
				})
				// #endif
			},
			handleTouch() {
			  // 更新最后一次触摸时间
			  this.$store.commit('lastTimeUpdata', new Date().getTime());
			},
		},
		onLaunch: function() {
			console.log('App Launch')
			let that = this;
			that.init();
			uni.setStorageSync('hasConfirm', false);
			// 监听触摸事件
			//uni.onTouchMove(this.handleTouch);
			//timeOut();
			// this.handleCheckVersion();
			// #ifdef APP-PLUS
			plus.push.addEventListener("click", function(msg) {
				console.log(msg);
				uni.switchTab({
					url:'/pages/index'
				})
			}, false);
			uni.getPushClientId({
				success: res => {
					console.log("客戶端，cid:"+res.cid);
				}
			});
			// #endif
			uni.onPushMessage((res) => {
				console.log("收到推送消息：",res) //监听推送消息
				if(res.type==='receive') {
					store.dispatch('ReceiveMessageNotice',res.data.title)
				}
			})
		},
		onShow: function() {
			//if (store.state.socketTask == null) {
			//	store.dispatch('websocketInit')
			//}
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		onExit() {
			if (this.timer) {
				clearInterval(timer)
				this.timer = null
			}
		}
	}
</script>

<style lang="scss">
	@import "uview-plus/index.scss";
	@import '@/static/scss/index.scss';
</style>